% GAUTIER LE BIHAN - 2020
% Replication files for �Shocks vs Menu Costs: Patterns of Price Rigidity in an Estimated Multi-Sector Menu-Cost Model� �Review of Economics and Statistics
%
% This code Plots  Appendix figure G based on stored  simulated data
clear;
tic

clc;
cd ..\..\Simu_identification\ident_canova_sala_CalvoPlus


load mat_results_CS
param=mat_results_CS;

moments=[ 0.0837  0.6737  0.0438  0.0698  4.4616];
 
actual_std=[ 0.0008  0.0042  0.0005  0.0010  0.1057]
actual_var= actual_std.* actual_std*1000;

 
m_target = moments';
m_scale=[actual_var'];
s=size(param,1);

for jj=1:s;
m_model=param(jj,6:10)';
G = m_target - m_model;
% % Weighting matrix W
size_weights = max(size(m_target));
scale = (ones(size_weights,1).*[4;1;1;1;1])./m_scale; 
% % take probability of default with 'double importance'
 W = eye(size_weights).*(ones(size_weights,1)*scale');
 clear scale size_weights
% 
 dist_sqrd = G.*G;
 W_vector = [W(1,1);W(2,2);W(3,3);W(4,4);W(5,5)];
dist_weighted = dist_sqrd.*W_vector;
% % Calculate new SMM estimator:
% 
 x(jj,1) = G'*W*G;
end;
param=[param x];

dist=param(:,11);
mylevel=-1*[ 0.004 0.013 0.033  0.05 0.07499]% 0.099]

param1=param((dist<0.065 & dist>0 ) ,:);
ncontour=4
paramopt=param((dist==min(dist((dist>0),1)) ),:);

param2=param1((param1(:,1)~= paramopt(1)),:);
dist2=param2(:,11);
paramopt2=param2((dist2==min(dist2) ),:);


M=[param1(:,2) param1(:,3) -1*param1(:,11)];
x = linspace(min(M(:,1)), max(M(:,1)), size(M,1)+1);
y = linspace(min(M(:,2)), max(M(:,2)), size(M,1)+1);
[X,Y] = meshgrid(x, y);
Z = griddata(M(:,1), M(:,2), M(:,3), X, Y);
figure(1)
subplot(2,2,3)
[C,h]=contourf(X, Y, Z, ncontour);
h.LevelList=mylevel;

map =  [
%ext�rieur   
 0.8 0.8 0.8
%2nd stage
      0.6 0.6 0.6
      0.6 0.6 0.6
      %3rd stage
      0.4 0.4 0.4 
      0.4 0.4 0.4 
      %Center
      0 0 0
       %0 0 1 
    
    ];
% map =  [
% %ext�rieur   
% 0 0.0 0.0
% %2nd stage
%       1 0 0.
%       1 0 0.
%       %3rd stage
%       00. 1 0.
%       00. 1 0.
%       %Center
%       00. 0 1
%        %0 0 1 
%     
%     ];
%surf(peaks)
colormap(map)
%colormap(pink(5))
cmap = colormap
x0=paramopt(:,2);
y0=paramopt(:,3);
%return;
sz=40;
c=[1 0 0];
hold on
scatter(x0, y0,sz,c, 'filled');
hold off
% hold on
% scatter(paramopt2(:,2), paramopt2(:,3),sz,c,'g', 'filled');
% hold off
%hold on
%scatter(x1, y1,sz,c, 'filled');
%hold off
xmu=[0.024 0.036]
ysig=[0.032 0.038]

%axis([xmu ysig]);
xlabel('\mu') % y-axis label
ylabel('\sigma') % x-axis label

%contour(X, Y, Z, 5, 'ShowText','on')
%axis equal
%grid



%figure(2)
%surf(X, Y, -1*Z)
%contour(X, Y, Z, 5, 'ShowText','on')
%axis equal
%grid

M=[param1(:,1) param1(:,2) -1*param1(:,11)];
x = linspace(min(M(:,1)), max(M(:,1)), size(M,1)+1);
y = linspace(min(M(:,2)), max(M(:,2)), size(M,1)+1);
[X,Y] = meshgrid(x, y);
Z = griddata(M(:,1), M(:,2), M(:,3), X, Y);
subplot(2,2,2)
[C,h]=contourf(X, Y, Z, ncontour);
h.LevelList=mylevel;
%colormap(pink)
map =  [
%ext�rieur   
 0.8 0.8 0.8
%2nd stage
      0.6 0.6 0.6
      0.6 0.6 0.6
      %3rd stage
      0.4 0.4 0.4 
      0.4 0.4 0.4 
      %Center
      0 0 0
       %0 0 1 
    
    ];
% map =  [
% %ext�rieur   
% 0 0.0 0.0
% %2nd stage
%       1 0 0.
%       1 0 0.
%       %3rd stage
%       00. 1 0.
%       00. 1 0.
%       %Center
%       00. 0 1
%        %0 0 1 
%     
%     ];
%surf(peaks)
colormap(map)
%colormap(pink(5))
x0=paramopt(:,1);
y0=paramopt(:,2);
%x1=.0501;
%y1=.0373;
sz=40
c=[1 0 0]
hold on
scatter(x0, y0,sz,c, 'filled');
hold off
% hold on
% scatter(paramopt2(:,1), paramopt2(:,2),sz,c,'g', 'filled');
% hold off
xlam=[0.046 0.054]
ymu=[0.024 0.036]
%axis([xlam ymu]);
%hold on
%scatter(x1, y1,sz,c, 'filled');
%hold off
xlabel('\lambda') % y-axis label
ylabel('\mu') % x-axis label
%contour(X, Y, Z, 5, 'ShowText','on')
%axis equal
%grid

%figure(4)
%surf(Y, X, -1*Z)


M=[param1(:,1) param1(:,3) -1*param1(:,11)];
x = linspace(min(M(:,1)), max(M(:,1)), size(M,1)+1);
y = linspace(min(M(:,2)), max(M(:,2)), size(M,1)+1);
[X,Y] = meshgrid(x, y);
Z = griddata(M(:,1), M(:,2), M(:,3), X, Y);
subplot(2,2,1)
%contour(X, Y, Z, ncontour)
[C,h]=contourf(X, Y, Z, ncontour);
h.LevelList=mylevel;
map =  [
%ext�rieur   
 0.8 0.8 0.8
%2nd stage
      0.6 0.6 0.6
      0.6 0.6 0.6
      %3rd stage
      0.4 0.4 0.4 
      0.4 0.4 0.4 
      %Center
      0 0 0
       %0 0 1 
    
    ];
% map =  [
% %ext�rieur   
% 0 0.0 0.0
% %2nd stage
%       1 0 0.
%       1 0 0.
%       %3rd stage
%       00. 1 0.
%       00. 1 0.
%       %Center
%       00. 0 1
%        %0 0 1 
%     
%     ];
%surf(peaks)
colormap(map)
%colormap(pink(5))
%colormap(pink)
x0=paramopt(:,1);
y0=paramopt(:,3);
%x1=.0501;
%y1=.0381;
sz=40
c=[1 0 0]

hold on
scatter(x0, y0,sz,c, 'filled');
hold off
% hold on
% scatter(paramopt2(:,1), paramopt2(:,3),sz,c,'g', 'filled');
% hold off
xlam=[0.046 0.054]
ysig=[0.032 0.038]
%axis([xlam ysig]);
%hold on
%scatter(x1, y1,sz,c, 'filled');
%hold off
xlabel('\lambda') % y-axis label
ylabel('\sigma') % x-axis label


 print('..\..\..\figures\Canova_CalvoPlus.pdf','-dpdf','-fillpage')
